package com.azusa.xbiao.service.impl;

import com.azusa.xbiao.dao.CartDao;
import com.azusa.xbiao.dao.CartMapper;
import com.azusa.xbiao.dao.GoodsDao;
import com.azusa.xbiao.dao.OrderDao;
import com.azusa.xbiao.entity.Cart;
import com.azusa.xbiao.entity.Goods;
import com.azusa.xbiao.entity.Order;
import com.azusa.xbiao.service.CartService;
import com.azusa.xbiao.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class CartServiceImpl implements CartService {

    @Autowired
    private CartDao cartDao;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public void insertGoodsToCart(Goods goods, Integer cid) {
        Cart cart = new Cart();
        cart.setId(cid);
        //查询购物车中是否有该商品
        Cart c = cartDao.findByIdAndGids(cid, goods.getId(),0);
        if (c != null){
            //在原来的基础上添加数量
            cart = c;
            cart.setCount(c.getCount() + goods.getCount());
        }else {
            cart.setCount(goods.getCount());
            cart.setPrice(goods.getPrice());
            cart.setIsPay((byte) 0);
        }
        cart.setAddDate(new Date());
        cart.setgIds(goods.getId());
        cart.setGoodsImage(goods.getCommoditUrl());
        cart.setgType(goods.getType());
        //保存购物车商品信息
        cartDao.save(cart);
    }

    @Override
    public Map<String, Object> initCart(Integer id,int isPay) {
        Map<String, Object> map = new HashMap<>();
        //查询购物车商品
        List<Cart> carts = cartDao.selectAll(id,isPay);
        //计算总金额
        BigDecimal totalPrice = new BigDecimal(0);
        for (Cart cart : carts) {
            BigDecimal count = new BigDecimal(cart.getCount());
            totalPrice = totalPrice.add(cart.getPrice().multiply(count));
        }
        //查询热门商品
        List<Goods> hot = goodsDao.selectHot();
        map.put("cart",carts);
        map.put("hot",hot);
        map.put("totalPrice",totalPrice);
        return map;
    }

    @Override
    public void settle(Cart cart) {
        //如果是已经处于待支付状态，则添加到订单表中
        if (cart.getIsPay() == 1){
            Order order = new Order();
            order.setuId(LoginUserUtil.getId());
            order.setgId(cart.getgIds());
            order.setCount(cart.getCount());
            order.setPrice(cart.getPrice());
            order.setArrival((byte) 0);
            order.setCreateDate(new Date());
            order.setEvaluate((byte) 0);
            order.setGoodsImageUrl(cart.getGoodsImage());
            order.setGoodsType(cart.getgType());
            orderDao.save(order);
            cartDao.delete(cart);
        }else{
            //查询待支付中是否已经包含此商品
            Cart isPry = cartDao.findByIdAndGids(cart.getId(), cart.getgIds(), 1);
            cart.setIsPay(1);
            if (isPry != null){
                //在原先的基础上增加数量
                cart.setCount(isPry.getCount() + cart.getCount());
                //删掉原先记录
                cartDao.delete(isPry);
            }
            cartDao.settle(cart.getId(),cart.getgIds(),1,cart.getCount());
        }
    }

    @Override
    public void settleAll(List<Cart> carts) {
        for (Cart cart : carts) {
            settle(cart);
        }
    }

    @Override
    public void delete(Cart cart) {
        cartDao.delete(cart);
    }
}
